"""
Case Type   : 资源池化
Case Name   : 使用set设置ss_enable_bcast_snapshot为on,合理报错
Create At   : 2024.1.3
Owner       : wx1133724
Description :
    1、查询ss_enable_bcast_snapshot默认值
    2、使用set设置ss_enable_bcast_snapshot=on
    3、恢复ss_enable_bcast_snapshot默认值
Expect      :
    1、默认值off
    2、合理报错
    3、恢复成功
"""

import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.com = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.param = 'ss_enable_bcast_snapshot'
        self.src = self.com.show_param(self.param)
        self.err_msg = 'cannot be changed without restarting the server'

        self.log.info('------检查数据库状态是否正常------')
        status = self.pri_sh.exec_cm_ctl('query', '-Cv')
        self.assertIn("cluster_state   : Normal", status)

    def test_shared_storage(self):
        text = '-----step1:查询ss_enable_bcast_snapshot默认值;' \
               'expect:默认值off-----'
        self.log.info(text)
        self.src = self.com.show_param(self.param)
        self.assertEqual('off', self.src, '执行失败' + text)

        text = '-----step2:使用set设置ss_enable_bcast_snapshot=on;' \
               'expect:合理报错-----'
        self.log.info(text)
        set_res = self.pri_sh.execut_db_sql(f'set {self.param} to on')
        self.log.info(set_res)
        self.assertIn(self.err_msg, set_res, '执行失败' + text)

        db_restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start', '-t 60')
        self.log.info(db_restart)
        self.assertIn(self.constant.cm_start_success_msg, db_restart,
                      '执行失败' + text)

        param_res = self.com.show_param(self.param)
        self.log.info(param_res)
        self.assertEqual('off', param_res, '执行失败' + text)

    def tearDown(self):
        text = '-----step3:恢复ss_enable_bcast_snapshot默认值;expect:恢复成功-----'
        self.log.info(text)
        recov_param = self.pri_sh.execute_gsguc('set',
                                              self.constant.GSGUC_SUCCESS_MSG,
                                              f"{self.param} = '{self.src}'")
        self.log.info(recov_param)

        self.log.info('-----重启集群-----')
        restart = self.pri_sh.exec_cm_ctl('stop && cm_ctl start')
        self.log.info(restart)

        self.assertTrue(recov_param, '参数修改失败')
        self.assertIn(self.constant.cm_start_success_msg, restart,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
